package com.hook3.pianke.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hook3.pianke.Net.Url;
import com.hook3.pianke.R;
import com.hook3.pianke.activity.base.BaseTagActivity;
import com.hook3.pianke.domain.TimeLinesTagsInfo;


import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;



/**
 * 碎片里的标签页面
 * Created by chai on 2015/12/12.
 */
public class TimeLinesTag extends Activity {
    public static final String TAG_NAME ="tag_name" ;
    private ImageView iv_back_title;
    private TextView title_name;
    private GridView gv_tag;

    private TimeLinesTagsInfo datas;
    private TimeLinesTagsInfo.DataEntity.ListEntity listEntity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.tag_timelines);
        MySetView();
        gv_tag = (GridView) findViewById(R.id.gv_tag);

        processData();
        gv_tag.setOnItemClickListener(new MyOnitemTagClickListener());




    }

    /**
     * 联网获取数据
     */
    private void processData() {
        RequestParams params = new RequestParams(Url.timeline_tags);
        params.addBodyParameter("client", "2");
        x.http().post(params, new Callback.CommonCallback<String>(){

            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                datas =  gson.fromJson(result, TimeLinesTagsInfo.class);
                Log.e("Tag", "解析的数据" + result);
                gv_tag.setAdapter(new MyTimeTagAdapter());



            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("Tag", "获取数据失败" + ex);

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 设置头
     */

    private void MySetView() {
        iv_back_title = (ImageView) findViewById(R.id.iv_back_title);
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("标签");
        iv_back_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    /**
     * 填充数据
     */
    private class MyTimeTagAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return  datas.getData().getList().size();

        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView==null){
                viewHolder = new ViewHolder();
                convertView = View.inflate(TimeLinesTag.this,R.layout.tag_timelines_tags,null);

                viewHolder.iv_tag_icon = (ImageView) convertView.findViewById(R.id.iv_tag_icon);


                viewHolder.tv_tag_name = (TextView) convertView.findViewById(R.id.tv_tag_name);
                viewHolder.tv_tag_count = (TextView) convertView.findViewById(R.id.tv_tag_count);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }
            listEntity = datas.getData().getList().get(position);
            x.image().bind(viewHolder.iv_tag_icon, listEntity.getImg());

            viewHolder.tv_tag_name.setText(listEntity.getTag());
            viewHolder.tv_tag_count.setText(listEntity.getCount()+"条");
            return convertView;
        }
    }
    static class ViewHolder{
        ImageView iv_tag_icon;
        TextView tv_tag_name;
        TextView tv_tag_count;

    }



    private class MyOnitemTagClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(TimeLinesTag.this,"点击了"+datas.getData().getList().get(position).getTag(),Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(TimeLinesTag.this,BaseTagActivity.class);
            startActivity(intent);
        }
    }
}
